package com.zznj.xzjc.activity.person;

import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.zznj.xzjc.R;
import com.zznj.xzjc.activity.LoginActivity;
import com.zznj.xzjc.application.APP;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.utils.FileUtils;
import com.zznj.xzjc.utils.NotificationUtil;

import java.io.File;
import java.text.DecimalFormat;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;


/**
 * Created by JY on 2017/11/15.
 */

public class PersonClearCacheActivity extends BaseActivity {

    @BindView(R.id.tcLogin)
    TextView tcLogin;
    @BindView(R.id.cacheSize)
    TextView cacheSize;


    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_clearcache_layout;
    }

    @Override
    public void initView() {
        setTitle("系统设置");
        tcLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                NotificationUtil no = new NotificationUtil(APP.getInstance().getApplicationContext());
                no.cancelAll();

                JPushInterface.stopPush(mContext);
                APP.getInstance().finishAllActivity();
                RxActivityTool.skipActivity(mContext, LoginActivity.class);
            }
        });

        File file = new File(FileUtils.ROOT_PATH);
        double size = getDirSize(file);
        DecimalFormat df = new DecimalFormat("######0.00");
        if(size == 0d){
            df = new DecimalFormat("######0");
        }
        String info = df.format(size);
        cacheSize.setText(info + "M");

        cacheSize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteAllFiles(file);
                cacheSize.setText("0M");
                RxToast.normal("清除成功！");
            }
        });
    }

    private void deleteAllFiles(File root) {
        File files[] = root.listFiles();
        if (files != null)
            for (File f : files) {
                if (f.isDirectory()) { // 判断是否为文件夹
                    deleteAllFiles(f);
                    try {
                        f.delete();
                    } catch (Exception e) {
                    }
                } else {
                    if (f.exists()) { // 判断是否存在
                        deleteAllFiles(f);
                        try {
                            f.delete();
                        } catch (Exception e) {
                        }
                    }
                }
            }
    }


    public static double getDirSize(File file) {
        //判断文件是否存在
        if (file.exists()) {
            //如果是目录则递归计算其内容的总大小
            if (file.isDirectory()) {
                File[] children = file.listFiles();
                double size = 0;
                for (File f : children)
                    size += getDirSize(f);
                return size;
            } else {//如果是文件则直接返回其大小,以“兆”为单位
                double size = (double) file.length() / 1024 / 1024;
                return size;
            }
        } else {
            System.out.println("文件或者文件夹不存在，请检查路径是否正确！");
            return 0.0;
        }
    }

    @Override
    public void initData() {

    }

    private void login(final String mobile, final String pwd) {


    }
}
